package com.swn.common.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONStringer;

/**
 * 处理json的工具类，负责将java数据转换成json
 * 
 */
public class JSONUtil {
	/**
	 * 从一个JSON 对象字符格式中得到一个java对象 说明：Bean的无参构造函数一定要写, 否则会报:
	 * net.sf.json.JSONException: java.lang.NoSuchMethodException
	 * 
	 * @param jsonString
	 * @param pojoCalss
	 * @return
	 */
	public static Object getObjectFromJsonString(String jsonString,
			Class pojoCalss) {
		Object pojo;
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		pojo = JSONObject.toBean(jsonObject, pojoCalss);
		return pojo;
	}

	/**
	 * 将java对象转换成json字符串
	 * 
	 * @param javaObj
	 * @return
	 */
	public static String getJsonStringFromObject(Object javaObj) {
		JSONObject json;
		json = JSONObject.fromObject(javaObj);
		return json.toString();
	}

	/**
	 * 从json HASH表达式中获取一个map
	 * 
	 * @param jsonString
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map getMapFromJsonString(String jsonString) {
		Map paramMap = new HashMap(); 
		if(jsonString!=null&&!"".equals(jsonString)){
			JSONObject jsonObject = JSONObject.fromObject(jsonString);//将json格式的字符串转换成json数据
			/*JSONArray jsonArray = JSONArray.fromObject(jsonString);*/
			Iterator keyIter = jsonObject.keys();//获取到jsonObject中的键；
			String key;//定义键
			Object value;//定义值
			
			while (keyIter.hasNext()) {
				key = (String) keyIter.next();
				value = jsonObject.get(key);
				if(value instanceof String){
					value = ((String) value).trim();
				}
				paramMap.put(key, value);//将key,value值放在map集合中
			}
		}
		return paramMap;//返回一个map集合
	}

	
	/**
	 * 从Map对象得到Json字串
	 * 
	 * @param map
	 * @return
	 */
	public static String getJsonStringFromMap(Map map) {
		JSONObject json = JSONObject.fromObject(map);
		return json.toString();
	}
	/**
	 * 字符串得到json对象
	 * 
	 * @param map
	 * @return
	 */
	public static JSONObject getJsonFromStr(String jsonstr) {
		JSONObject json = JSONObject.fromObject(jsonstr);
		return json;
	}
	/**
	 * 从json字串中得到相应java数组
	 * 
	 * @param jsonString
	 *            like "[\"李斯\",100]"
	 * @return
	 */
	public static Object[] getObjectArrayFromJsonString(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		return jsonArray.toArray();
	}

	/**
	 * 将list转换成String
	 * 
	 * @param list
	 * @return String
	 */
	public static String getJsonStringFromList(List list) {
		JSONArray jsonArray = JSONArray.fromObject(list);
		return jsonArray.toString();
	}

	/**
	 * 用JSONStringer构造一个JsonString
	 * 
	 * @param m
	 * @return
	 */
	public static String buildJsonString(Map m) {
		JSONStringer stringer = new JSONStringer();
		stringer.object();
		for (Object key : m.keySet()) {
			stringer.key((String) key).value((String) m.get(key));
		}
		stringer.key("phone");
		// begin nesting a array
		stringer.array();
		stringer.value("13998098000");
		stringer.value("8765432");
		// nestring object in array
		stringer.object();
		stringer.key("ppcall");
		stringer.value(53881);
		stringer.endObject();
		stringer.value("13990980980");
		// end nesting a array
		stringer.endArray();

		stringer.endObject();
		return stringer.toString();
	}

	public static void printMap(Map map) {
		for (Object key : map.keySet()) {
			System.out.println(key + ":" + map.get(key));
		}
	}

	public static void main(String[] args) {
		Map m = new HashMap() {
			{
				put("JSon", "HelloWorld");
				put("Flex", "Ok");
			}
		};
		//System.out.println(buildJsonString(m));
		/*System.out.println(new JSONStringer().object().key("JSON")
				.value("Hello, World!").key("Flex").value("OK").endObject()
				.toString());*/
		
		System.out.println(getMapFromJsonString("{amount='2',commodity_url='http://192.168.4.17:8888/group1/M00/00/05/wKgEEVmCzT2EWHRHAAAAAMqwihw412.jpg',commodity_id=0ca326c502d94903802996aff6f721c6,specification_value_id=2-14-12,price=100.00,spe_name='10盒,红色,100ml',store_count=100,commodity_name='益生MT'}"));
	}

	/**功能说明:将前台封装的json列表中的null对象替换为""
	 *	
	 * @author  : Leon
	 * @date    : 2016-5-6 下午2:23:18
	 * @modifier: 
	 * @mod_Date: 
	 * @param resultMap
	 * @return
	 */
	public static Map<String, Object> removeNullable(Map<String, Object> resultMap) {
		if (resultMap == null || resultMap.isEmpty()) return Collections.emptyMap();
		ConcurrentHashMap<String, Object> map = new ConcurrentHashMap<String,Object>();
		map.putAll(resultMap);
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			if (entry.getValue() == null || entry.getValue().equals("null")) {
//				resultMap.remove(entry.getKey());
				resultMap.put(entry.getKey(),"");
			}
		}
		return resultMap;
	}
}